// pages/login/login.js

import { getPassword, getSchoolid, getUserName, setPassword, setSchoolid, setUserName } from "../../utils/store";

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  isShow:false,
  data: {
    schoolItems:[],
    index:0,
    user : {loginid:'admin',passwd:'123456',username:'admin'}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user:{loginid:getUserName(),passwd:getPassword(),username:getUserName()}
    })
    this.getAllSchool();
    //this.getShow();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  setLoginId(e){
    this.setData({
      'user.loginid' : e.detail.value
    })
    setUserName(e.detail.value)
  },
  setPassword(e){
    this.setData({
      'user.passwd' : e.detail.value
    })
    setPassword(e.detail.value)
  },
  login(){
    const schoolid = this.data.schoolItems[this.data.index].schoolid
    this.setData({
      'user.schoolid' : schoolid
    })
    setSchoolid(schoolid)
    app.doLogin();
  },
  login2(){
    wx.getUserInfo({
      success: function(res) {
        console.log(res)
        var userInfo = res.userInfo
        var nickName = userInfo.nickName
        var avatarUrl = userInfo.avatarUrl
        var gender = userInfo.gender //性别 0：未知、1：男、2：女
        var province = userInfo.province
        var city = userInfo.city
        var country = userInfo.country
      }
    })
  },
  addImg(img){
    
  },
  faceLogin(){
    wx.navigateTo({
      url: '/pages/face/face',
    })
  },
  getAllSchool(){
    const schoolid = getSchoolid()
    app.api.getSchools().then(res=>{
      if(schoolid){
        res.data.forEach((item,inIndex)=>{
          if(item.schoolid === schoolid){
            this.setData({
              schoolItems:res.data,
              index:inIndex
            })
          }
        })
      }else{
        this.setData({
          schoolItems:res.data,
          index:0
        })
      }
    })
  },
  PickerChange(e){
    this.setData({
      index:e.detail.value
    })
  }
})